import {appVersion} from '../../../utils/config';

Page({
    data: {
        version: "" as string,
        categories: [
            {
                id: 1,
                name: "2026@体系化知识学习",
                desc: "2026版 | 共17章节",
            },
            {
                id: 2,
                name: "专题专练",
                desc: "英语题 | 计算题",
            },
            {
                id: 3,
                name: "错题专练",
                desc: "智能练习 | 查漏补缺",
            },
            {
                id: 4,
                name: "选择题",
                desc: "近2年 | 共4套",
            },
            {
                id: 5,
                name: "案例题",
                desc: "找茬 | 计算 | 画图",
            },
            {
                id: 6,
                name: "论文题",
                desc: "近5年 | 共14篇",
            }
        ],
        firstLayer: [],
        secondLayer: [],
        thirdLayer: [],
        // 添加统计数据
        statistics: {
            studyHours: 'xxx' as string | number,
            questionCount: 'xxx' as string | number,
            correctRate: 'xx%' as string
        }
    },

    onLoad() {
        // 将分类分成三层
        this.setData({
            version: appVersion,
            firstLayer: this.data.categories.slice(0, 1),
            secondLayer: this.data.categories.slice(1, 3),
            thirdLayer: this.data.categories.slice(3,6)
        });

        // 获取统计数据
        this.fetchStatistics();
    },

    // 获取统计数据
    async fetchStatistics(): Promise<any> {
        const app = getApp<IAppOption>();

        return new Promise((resolve, reject) => {
            wx.request({
                url: app.buildApiUrl('/api/exam/statistics'),
                method: 'GET',
                header: {'Authorization': wx.getStorageSync('token')},
                success: (res: any) => {
                    if (res.statusCode === 200) {
                        const data = res.data.data;
                        // 计算学习小时数，这里假设每次答题平均花费3分钟
                        const studyHours = Math.round(data.questionCount * 3 ) / 60;
                        // 计算正确率
                        const correctRate = data.questionCount > 0
                            ? Math.round((data.correctCount / data.questionCount) * 1000) / 10 + '%'
                            : '0%';

                        this.setData({
                            statistics: {
                                studyHours: studyHours,
                                questionCount: data.questionCount,
                                correctRate: correctRate
                            }
                        });
                    }
                },
                fail: (err) => {
                    console.error('获取统计数据失败', err);
                }
            });
        })
    },

    navigateToCategory(e: any) {
        const categoryId = e.currentTarget.dataset.id;
        if (categoryId == 3) {
            // 错题专练直接跳转到练习页面
            wx.navigateTo({
                url: '/pages/exam/exam-wrong/exam-wrong'
            });
            return;
        }
        
        // if (categoryId == 5) {
        //     wx.showToast({
        //         title: '当前分类暂未开通，敬请期待',
        //         icon: 'none',
        //         duration: 2000
        //     });
        //     return;
        // }
        wx.navigateTo({
            url: `/pages/exam/exam-category/exam-category?id=${categoryId}`
        });
    }
});